package com.tuanzi.loan.web.bean.collaborate;

import com.google.common.collect.Maps;
import com.tuanzi.loan.business.entity.common.CommProductChannel;
import com.tuanzi.loan.business.enums.CollaborateColShowEnum;
import com.tuanzi.loan.business.service.collaborate.CollaborateChannelRatioService;
import com.tuanzi.loan.business.service.common.CommProductChannelService;
import com.tuanzi.loan.vo.request.Collaborate.CollaborateStatListRequest;
import com.tuanzi.loan.vo.response.collaborate.CollaborateStatListResponse;
import com.tuanzi.loan.web.configuration.JSFListingBean;
import com.tuanzi.loan.web.lazy.collaborate.CollaborateStatListLazyDataModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * User: liangZongXi
 * Create time: 14:03 2018/9/19
 * Description:
 */
@Data
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class CollaborateStatListBean extends JSFListingBean<CollaborateStatListRequest, CollaborateStatListResponse> {
    private static final long serialVersionUID = -4748737781848057628L;

    @Autowired
    private CollaborateChannelRatioService collaborateChannelRatioService;
    @Autowired
    private CommProductChannelService commProductChannelService;

    private Set<Integer> showTypeSet;
    private List<CommProductChannel> currentUserChannelList;
    private Map<Integer,String> productMap = Maps.newHashMap();

    @PostConstruct
    private void init(){
        initLazyDataModel();
        currentUserChannelList = commProductChannelService.findCurrentUserChannels();
        showTypeSet = collaborateChannelRatioService.findCurrentUserShowType(currentUserChannelList);
        productMap.put(8000,"菠萝贷主包");
        productMap.put(8002,"菠萝贷IOS");
    }

    public void freshData(){
        Boolean result = collaborateChannelRatioService.updateCollaborateData();
        if(result){
            addInfoMessage("操作成功，此操作5分钟之内生效");
        }else {
            addErrorMessage("刷新失败,请稍后再试");
        }
    }

    private void initLazyDataModel(){
        $ = new CollaborateStatListRequest();
        lazyDataModel = new CollaborateStatListLazyDataModel($);
    }

    public void onRest(){
        initLazyDataModel();
    }

    public Boolean showRegister(){
        return showTypeSet.contains(CollaborateColShowEnum.H5_REGISTER.getId())
                || showTypeSet.contains(CollaborateColShowEnum.ALL.getId());
    }

    public Boolean showNewRegister(){
        return showTypeSet.contains(CollaborateColShowEnum.H5_NEW_REGISTER.getId())
                || showTypeSet.contains(CollaborateColShowEnum.ALL.getId());
    }

    public Boolean showAppCount(){
        return showTypeSet.contains(CollaborateColShowEnum.APP_COUNT.getId())
                || showTypeSet.contains(CollaborateColShowEnum.ALL.getId());
    }

    public Boolean showAppCountPhone(){
        return showTypeSet.contains(CollaborateColShowEnum.APP_COUNT_PHONE.getId())
                || showTypeSet.contains(CollaborateColShowEnum.ALL.getId());
    }

    @Override
    public String translateProductChannel(Integer channelCode) {
        return currentUserChannelList.parallelStream().filter(x -> x.getChannelCode().equals(channelCode)).map(CommProductChannel::getChannelName).findFirst()
                .orElse(channelCode.toString());
    }

    @Override
    public String translateProduct(Integer productId) {
        String s = productMap.get(productId);
        return StringUtils.isNotBlank(s) ? s : productId.toString();
    }
}
